package cn.com.xiaoshoujia.listener.controller;

import cn.com.xiaoshoujia.quartz.*;
import org.quartz.*;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


@Component
public class StartupListener implements ApplicationContextAware {

    @Resource(name = "scheduler")
    private Scheduler scheduler;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        String time = String.valueOf(System.currentTimeMillis());
        String jobName = "job" + time;
        String triggerName = "trigger" + time;
        JobKey jobKey = JobKey.jobKey(jobName, "group");
        JobDetail job = JobBuilder.newJob(TeamRefundTask.class).withIdentity(jobKey).build();
        /*退款*/
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerName, "group")
                .withSchedule(CronScheduleBuilder.cronSchedule("0 0/10 * * * ?"))
                .build();

        String jobQueryName = "jobQuery" + time;
        String triggerQueryName = "triggerQuery" + time;
        JobKey jobQueryKey = JobKey.jobKey(jobQueryName, "group");
        JobDetail jobQuery = JobBuilder.newJob(TeamRefundQueryTask.class).withIdentity(jobQueryKey).build();
        /*查询退款*/
        Trigger triggerQuery = TriggerBuilder.newTrigger().withIdentity(triggerQueryName, "group")
                .withSchedule(CronScheduleBuilder.cronSchedule("0 0/15 * * * ?"))
                .build();

        JobKey walletErrorLogsKey = JobKey.jobKey("jobWalletErrorLogs", "group");
        JobDetail jobWalletErrorLogs = JobBuilder.newJob(WalletErrorLogsTask.class).withIdentity(walletErrorLogsKey).build();
        /*查询钱包服务错误日志*/
        Trigger triggerWalletErrorLogs = TriggerBuilder.newTrigger().withIdentity("triggerWalletErrorLogs", "group")
                .withSchedule(CronScheduleBuilder.cronSchedule("0 0/1 * * * ?"))
                .build();

        JobKey wxMsgKey = JobKey.jobKey("jobWxMsg", "group");
        JobDetail jobWxMsg = JobBuilder.newJob(WxMsgTask.class).withIdentity(wxMsgKey).build();
        /*查询微信支付响应信息记录*/
        Trigger triggerWxMsg = TriggerBuilder.newTrigger().withIdentity("triggerWxMsg", "group")
                .withSchedule(CronScheduleBuilder.cronSchedule("0 0/1 * * * ?"))
                .build();

        JobKey settlementErrorKey = JobKey.jobKey("jobSettlementError", "group");
        JobDetail jobSettlementError = JobBuilder.newJob(SettlementErrorTask.class).withIdentity(settlementErrorKey).build();
        /*钱包服务结算异常处理*/
        Trigger triggerSettlementError = TriggerBuilder.newTrigger().withIdentity("triggerSettlementError", "group")
                .withSchedule(CronScheduleBuilder.cronSchedule("0 0/1 * * * ?"))
                .build();

        try {
            scheduler.scheduleJob(job, trigger);
            scheduler.scheduleJob(jobQuery, triggerQuery);
            scheduler.scheduleJob(jobWalletErrorLogs, triggerWalletErrorLogs);
            scheduler.scheduleJob(jobWxMsg, triggerWxMsg);
            scheduler.scheduleJob(jobSettlementError, triggerSettlementError);
            scheduler.start();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}
